﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Configuration;

namespace devtm.Editor.Configuration.Data
{


    public class TemplateMethod : ConfigProperty
    {
        //private MethodInfo methodInfo;   

        public TemplateMethod(MethodInfo methodInfo, ElementSection instance, string name)
        {
            // TODO: Complete member initialization
            this.MemberInfo = methodInfo;
            this.Instance = instance;
            this.Name = name ?? base.Name;
            this.PropertyName = base.PropertyName;
        }



        public TemplateMethod(Action action, ElementSection section, string name)
        {
            // TODO: Complete member initialization
            this.Action = action;
            this.Instance = section;
            this.PropertyName = name;
            this.Name = name;
        }

        public override string Name { get; set; }
        public override string PropertyName { get; set; }

        //public override 

        public override void RunEditor()
        {

            //Datas.IsModified = true;

            if (MemberInfo != null)
            {
                MethodInfo m = (MemberInfo as MethodInfo);

                if (m.GetParameters().Count() == 0)
                    m.Invoke(this.Instance, null);
                else
                    m.Invoke(this.Instance, new Object[] { this });

            }

            else if (Action != null)
                Action();


            ConfigurationEventHandlerEvent(this, this.Instance);

            ValueHasChanged();

        }

        


        public override void Selector()
        {
            throw new NotImplementedException();
        }

        public Action Action { get; set; }

    }

}
